//压缩图片并上传
function readFile(file,id,c){
    var file2 = file.files[0] //读取文件流
    let reader = new FileReader()
    reader.readAsDataURL(file2)
    reader.onload = function(e) {
        let base64 = e.target.result
        let image = new Image() //新建一个img标签（还没嵌入DOM节点)
        image.src = base64
        image.onload = function(){
            let canvas = document.createElement('canvas'),
                context = canvas.getContext('2d'),
                imageWidth = image.width / 2,    //压缩后图片的大小
                imageHeight = image.height / 2,
                data = ''

            canvas.width = imageWidth
            canvas.height = imageHeight

            context.drawImage(image, 0, 0, imageWidth, imageHeight)
            data = canvas.toDataURL(file2.type)
            $(c).attr({'src':data})
            let fpn = dataURLtoFile(data,'file.jpg')
            let formData = new FormData()
            formData.append('file',fpn);
            $.ajax({
                url: ctx+'common/upload',
                type: 'POST',
                cache: false,
                data: formData,
                dataType:'json',
                processData: false,
                contentType: false,
                success:function(res){
                    if(res.code ===0){
                        $(id).val(res.fileName)
                        console.log(c)
                        $(c).attr('src',res.fileName)
                        console.log($(c))
                        mui.toast(res.msg, {duration: 'long', type: 'div'})
                    }

                }
            });
        }
    }
}
function dataURLtoFile(dataurl, filename = 'file') {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let suffix = mime.split('/')[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], `${filename}.${suffix}`, {
        type: mime
    })
}
//领取任务列表
function receiveTask(taskId) {
    mui.ajax(ctx+'front/receiveTask', {
        data: {'taskId': taskId},
        dataType: 'json',//服务器返回json格式数据
        type: 'post',//HTTP请求类型
        timeout: 10000,//超时时间设置为10秒；
        headers: {'Content-Type': 'application/json'},
        success: function (res) {
            mui.toast(res.msg, {duration: 'long', type: 'div'})

            setInterval(function () {
                if(res.code==500&&res.msg=='抖音资料没填写或者没通过审核，不能接任务！'){
                    window.location.href =  ctx+'front/toBind'
                }

                if(res.code == 0){
                    window.location.href = ctx +'front/toTasksRecords/1'
                }
            }, 1000)
        },
        error: function (xhr, type, errorThrown) {
            //异常处理；
            console.log(type);
        }
    });
}
/**
 * 任务类型
 * @param type
 * @returns {string}
 */
function formatterWay(type) {
    return type === 1 ? "点赞" : type === 2 ? "关注" : type === 3 ? "互粉" : type === 4 ? "推荐奖" : type === 5 ? "团队奖" : type === 6 ? "购买套餐" : '';
}



/**
 * 短信发送
 * @returns {string}
 * @param phone
 */
function sendMsg(phone) {
    mui.ajax(ctx+'front/release/sendMsg', {
        data: {'phone': phone},
        dataType: 'json',//服务器返回json格式数据
        type: 'post',//HTTP请求类型
        timeout: 60000,//超时时间设置为10秒；
        headers: {'Content-Type': 'application/json'},
        success: function (res) {
            mui.toast(res.msg, {duration: 'long', type: 'div'})
        },
        error: function (xhr, type, errorThrown) {
            //异常处理；
            console.log(type);
        }
    });
}